//
//  HTMLocationManager.m
//  QVBusUser
//
//  Created by Ting Han on 2017/8/16.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTMLocationManager.h"
NSString * const kdidUpdateLocation = @"didUpdateLocation";
@interface HTMLocationManager ()<AMapLocationManagerDelegate>


@end
@implementation HTMLocationManager
static HTMLocationManager *manager = nil;

+ (instancetype)shareManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[HTMLocationManager alloc] init];
    });
    return manager;
}
- (NSString *)getCurrentAddress{
    if (self.currentProvince == nil || self.currentProvince.length <= 0 || self.currentCity == nil || self.currentCity.length <= 0) {
        return @"";
    }
    if ([self.currentProvince isEqualToString:self.currentCity]) {
        if ([[self.currentProvince substringFromIndex:self.currentProvince.length-1] isEqualToString:@"省"] || [[self.currentProvince substringFromIndex:self.currentProvince.length-1] isEqualToString:@"市"]) {
            return [self.currentProvince substringToIndex:self.currentProvince.length - 1];
        }else{
            return self.currentProvince;
        }
    }else{
        NSString *province = self.currentProvince;
        if ([[self.currentProvince substringFromIndex:self.currentProvince.length-1] isEqualToString:@"省"] || [[self.currentProvince substringFromIndex:self.currentProvince.length-1] isEqualToString:@"市"]) {
            province = [self.currentProvince substringToIndex:self.currentProvince.length - 1];
        }
        NSString *city = self.currentCity;
        if ([[self.currentCity substringFromIndex:self.currentCity.length-1] isEqualToString:@"市"]) {
            city = [self.currentCity substringToIndex:self.currentProvince.length - 2];
        }
        return [NSString stringWithFormat:@"%@%@",province,city];
    }
}
- (instancetype)init
{
    if (self == [super init]) {
        _locationService = [[AMapLocationManager alloc] init];
        _locationService.delegate = self;
        [_locationService setPausesLocationUpdatesAutomatically:NO];
        _locationService.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        _locationService.distanceFilter = kCLDistanceFilterNone;
        _locationService.locatingWithReGeocode = YES;
        self.locationService.locationTimeout =2;
        self.locationService.reGeocodeTimeout = 2;
      
    }
    return self;
}

+ (instancetype)singleInstance
{
    return [self shareManager];
}

+ (void)startLocation
{
    HTMLocationManager *shareMag = [self shareManager];
    [shareMag.locationService startUpdatingLocation];
}

+ (void)stopLocation
{
    HTMLocationManager *shareMag = [self shareManager];
    [shareMag.locationService stopUpdatingLocation];
}

- (void)requestLocationCompletionBlock:(void (^)(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error))completionBlock{
    
    [self.locationService requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (completionBlock) {
            completionBlock(location,regeocode,error);
        }
    }];
    
}


- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode{
    
    if (location == nil) {
        return;
    }
    //逆地理信息
    if (reGeocode)
    {
        DBLog(@"reGeocode:%@", reGeocode);
        NSString *str = reGeocode.city;

    }
    
    //    [[NSNotificationCenter defaultCenter] postNotificationName:kdidUpdateLocation object:location];
    
}



@end
